/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.io.WTKX;

import jbxml.Dictionary;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;

/**
 * @author gbrown
 * @author jdp
 */
public class Alert
    extends wtkx.in.Script
{
    public enum MessageType {
        Stop(IconUserBusy),
        Go(IconUserOnline),
        Timeout(IconUserAway),
        Broken(IconUserOffline);


        public final URL icon;

        private MessageType(URL icon){
            this.icon = icon;
        }

        public static MessageType decode(String value) {
            return valueOf(jbxml.Resolver.ClassCamel(value));
        }
    }

    public final static String ToString(Throwable thrown){
        StringWriter buf = new StringWriter();
        PrintWriter err = new PrintWriter(buf);
        thrown.printStackTrace(err);
        return buf.toString();
    }



    public final MessageType type;

    public final String message;


    public Alert(MessageType type, String message){
        super();
        if (null != type){
            this.type = type;
            if (null != message)
                this.message = message;
            else
                throw new IllegalArgumentException();
        }
        else
            throw new IllegalArgumentException();
    }

    public Alert(MessageType type, Throwable thrown){
        super();
        if (null != type){
            this.type = type;
            if (null != thrown)
                this.message = ToString(thrown);
            else
                throw new IllegalArgumentException();
        }
        else
            throw new IllegalArgumentException();
    }

	public void startup(Scene scene, Dictionary<String, String> properties)
		throws java.io.IOException 
    {
        this.scene = scene;
        this.properties = properties;

        properties.put("alertIcon",this.type.icon.toExternalForm());

        properties.put("alertMessage",this.message);

        URL url = this.getClass().getResource("Alert.wtkx");

        this.window = scene.open(url);

        this.window.init(this);
	}
}
